﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentTypeService : BaseService, IEquipmentTypeService
    {
        private readonly IEquipmentTypeRepository _equipmentTypeRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentTypeService(IMapper mapper, IEquipmentTypeRepository _equipmentTypeRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentTypeRepository = _equipmentTypeRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentType(EquipmentType equipmentType)
        {
            _equipmentTypeRepository.AddEntity(equipmentType);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentType(long id)
        {
            var cust = _equipmentTypeRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentTypeRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentType> GetEquipmentType(string? TypeName, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentTypeRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(TypeName))
            {
                custList = custList.Where(c => c.TypeName.Contains(TypeName));
            }
            var list = _equipmentTypeRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentType? GetEquipmentTypeById(long id)
        {
            return _equipmentTypeRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentType(EquipmentType EquipmentType)
        {
            var aobj = _equipmentTypeRepository.QueryIsEntityWhere(x => x.Id == EquipmentType.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentTypeRepository.UpdEntity(EquipmentType);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
